package com.anjbo.utils;

import org.apache.commons.lang3.StringUtils;

import com.anjbo.common.RedisKey;
import com.anjbo.common.RedisOperator;


/**
 * 
 * @author Kevin Chang
 * 
 */
public class LoginCacheUtil {
	public static final String UID = "uid";
	public static final String LOGINTIME = "loginTime";
	public static final String USERTYPE = "userType";
	public static final String LOGINTYPE = "loginType";
	public static final String MOBILE = "mobile";
	public static final String TEAM = "team";
	public static final String NAME = "name";
	public static final String BANKID = "bankId";
	public static final String VALID = "valid";
	public static final String HEADIMG = "headImg";
	public static final String AGENCYID = "agencyId";

	/**
	 * 获取登录的key
	 * 
	 * @param baseDto
	 * @return
	 */
	public static String createLoginKey(String sid, String deviceId) {
		if (StringUtils.isEmpty(sid) || StringUtils.isEmpty(deviceId)) {
			return null;
		}
		return RedisKey.PREFIX.MORTGAGE_LOGININFO + sid + RedisKey.SPLIT_FLAG
				+ deviceId;
	}

	/**
	 * 获取userid
	 * 
	 * @param authParams
	 * @return
	 */
	public static String getUid(String sid, String deviceId) {
		if (StringUtils.isEmpty(sid) || StringUtils.isEmpty(deviceId)) {
			return "";
		}
		String key = createLoginKey(sid, deviceId);
		String uid = RedisOperator.getStringFromMap(key, UID);
		return uid;
	}

	/**
	 * 获取用户类型
	 * 
	 * @param authParams
	 * @return
	 */
	public static String getUserType(String sid, String deviceId) {
		if (StringUtils.isEmpty(sid) || StringUtils.isEmpty(deviceId)) {
			return null;
		}
		String key = createLoginKey(sid, deviceId);
		String userType = RedisOperator.getStringFromMap(key, USERTYPE);
		return userType;
	}

	/**
	 * 获取用户手机号码
	 * 
	 * @param authParams
	 * @return
	 */
	public static String getMobile(String sid, String deviceId) {
		if (StringUtils.isEmpty(sid) || StringUtils.isEmpty(deviceId)) {
			return null;
		}
		String key = createLoginKey(sid, deviceId);
		String mobile = RedisOperator.getStringFromMap(key, MOBILE);
		return mobile;
	}

	/**
	 * 
	 * @param sid
	 * @param deviceId
	 * @return
	 */
	public static String getBankId(String sid, String deviceId) {
		if (StringUtils.isEmpty(sid) || StringUtils.isEmpty(deviceId)) {
			return null;
		}
		String key = createLoginKey(sid, deviceId);
		String bankId = RedisOperator.getStringFromMap(key, BANKID);
		return bankId;
	}

	/**
	 * @param sid
	 * @param deviceId
	 * @return
	 */
	public static String getName(String sid, String deviceId) {
		if (StringUtils.isEmpty(sid) || StringUtils.isEmpty(deviceId)) {
			return null;
		}
		String key = createLoginKey(sid, deviceId);
		String name = RedisOperator.getStringFromMap(key, NAME);
		return name;
	}

	/**
	 * 
	 * @param sid
	 * @param deviceId
	 * @return
	 */
	public static String getTeam(String sid, String deviceId) {
		if (StringUtils.isEmpty(sid) || StringUtils.isEmpty(deviceId)) {
			return null;
		}
		String key = createLoginKey(sid, deviceId);
		String mobile = RedisOperator.getStringFromMap(key, TEAM);
		return mobile;
	}

	/**
	 * 注销用户登录信息，考虑到多设备登录问题，多设备登录key与deviceId有关
	 * 
	 * @param authParams
	 * @return
	 */
	public static boolean delLoginInfo(String sid, String deviceId) {
		if (StringUtils.isEmpty(sid) || StringUtils.isEmpty(deviceId)) {
			return false;
		}
		String key = createLoginKey(sid, deviceId);
		// 直接根据key删除
		RedisOperator.delete(key);
		return true;
	}
	
	/**
	 * 获取用户手机号码
	 * 
	 * @param authParams
	 * @return
	 */
	public static String getHeadImg(String sid, String deviceId) {
		if (StringUtils.isEmpty(sid) || StringUtils.isEmpty(deviceId)) {
			return null;
		}
		String key = createLoginKey(sid, deviceId);
		String headImg = RedisOperator.getStringFromMap(key, HEADIMG);
		return headImg;
	}
}
